package io.github.thundzeng.miniemail.starter.autoconfigure;

import io.github.thundzeng.miniemail.core.MiniEmail;
import io.github.thundzeng.miniemail.core.MiniEmailFactory;
import io.github.thundzeng.miniemail.core.MiniEmailFactoryBuilder;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 建立 MiniEmail
 *
 * @author thunzeng
 */
@Configuration
@EnableConfigurationProperties(value = {MiniEmailProperties.class})
public class MiniEmailAutoConfiguration {

    @ConditionalOnMissingBean(MiniEmail.class)
    @Bean(value = "miniEmail")
    public MiniEmail miniEmail(MiniEmailProperties properties) {
        // 构建工厂类
        MiniEmailFactory factory = new MiniEmailFactoryBuilder().build(properties);
        // 初始化
        MiniEmail init = factory.init();
        return init;
    }

}
